package com.zorn.platform.global;

import static com.alibaba.fastjson.util.TypeUtils.castToDate;
import static com.alibaba.fastjson.util.TypeUtils.castToSqlDate;
import static com.alibaba.fastjson.util.TypeUtils.castToTimestamp;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.springframework.web.context.ContextLoader;

import com.zorn.core.util.ResultUtils;
import com.zorn.platform.setting.dao.SettingDao;

public class Setting {

	private static Map<String, Object> settings = new HashMap<String, Object>();

	static {
		SettingDao bean = ContextLoader.getCurrentWebApplicationContext().getBean(SettingDao.class);
		settings = ResultUtils.toMap(bean.query(settings), "key", "value");
	}

	protected Setting() {
	}

	public static final Object get(String key) {
		return settings.get(key);
	}

	public static final Boolean getBoolean(String key) {
		return MapUtils.getBoolean(settings, key);
	}

	public static final Boolean getBoolean(String key, Boolean def) {
		return MapUtils.getBoolean(settings, key, def);
	}

	public static final boolean getBooleanValue(String key) {
		return MapUtils.getBoolean(settings, key);
	}

	public static final boolean getBooleanValue(String key, boolean def) {
		return MapUtils.getBooleanValue(settings, key);
	}

	public Integer getInteger(String key) {
		return MapUtils.getInteger(settings, key);
	}

	public Integer getInteger(String key, Integer def) {
		return MapUtils.getInteger(settings, key, def);
	}

	public int getIntValue(String key) {
		return MapUtils.getIntValue(settings, key);
	}

	public int getIntValue(String key, int def) {
		return MapUtils.getIntValue(settings, key, def);
	}

	public static final String getString(String key) {
		return MapUtils.getString(settings, key);
	}

	public static final String getString(String key, String def) {
		return MapUtils.getString(settings, key, def);
	}

	public static final Date getDate(String key) {
		return getDate(key, null);
	}

	public static final Date getDate(String key, Date def) {
		Object value = get(key);

		if (value == null) return def;

		return castToDate(value);
	}

	public static final java.sql.Date getSqlDate(String key) {
		return getSqlDate(key, null);
	}

	public static final java.sql.Date getSqlDate(String key, java.sql.Date def) {
		Object value = get(key);

		if (value == null) return def;

		return castToSqlDate(value);
	}

	public static final java.sql.Timestamp getTimestamp(String key) {
		return getTimestamp(key, null);
	}

	public static final java.sql.Timestamp getTimestamp(String key, java.sql.Timestamp def) {
		Object value = get(key);

		if (value == null) return def;

		return castToTimestamp(value);
	}

	public static final Map<String, Object> getSettings() {
		return getSettings(true);
	}

	public static final Map<String, Object> getSettings(boolean clone) {
		return clone ? new HashMap<String, Object>(settings) : settings;
	}

	public static final void setSettings(Map<String, Object> settings) {
		Setting.settings = settings;
	}

}
